package com.bjsxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.mapper.RoleMenuMapper;
import com.bjsxt.pojo.PageResult;
import com.bjsxt.pojo.Result;
import com.bjsxt.pojo.Role;
import com.bjsxt.mapper.RoleMapper;
import com.bjsxt.pojo.RoleMenu;
import com.bjsxt.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.interfaces.RSAKey;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-02-22
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    //声明mapper层属性
    @Autowired
    private RoleMapper roleMapper;
    @Autowired RoleMenuMapper roleMenuMapper;
    //分页加载数据
    @Override
    public PageResult<Role> selectRoleInfo(Integer page, Integer rows) {
        //1、创建分页对象，存储分页信息
        IPage<Role> p=new Page<>(page,rows);
        //2、分页查询
        IPage<Role> roleIPage = roleMapper.selectPage(p, null);
        //获取分页的角色信息
        List<Role> rs=roleIPage.getRecords();
        //遍历获取角色的菜单的ID集合
        for (Role r:rs){
            //创建条件构造器
            QueryWrapper<RoleMenu> queryWrapper=new QueryWrapper<>();
            queryWrapper.select("mid").eq("rid",r.getRid());
            List<Object> li = roleMenuMapper.selectObjs(queryWrapper);
            //将菜单id的集合存储到角色中
            r.setMids(li);
        }
        //3、将结果封装到PageResult中
        return new PageResult(roleIPage.getTotal(),roleIPage.getRecords());
    }
    //增加角色信息
    @Override
    public Result insertRoleInfoService(String mids, Role role) {
         //1、增加角色
        boolean insert = role.insert();
        //2、获取增加角色的菜单Id
        if (!"".equals(mids)&&mids!=null){
            String[] mid = mids.split(",");
            //3、获取增加的角色的Id
            Integer rid = role.getRid();
            for (String m:mid){
                RoleMenu roleMenu=new RoleMenu();
                roleMenu.setRid(rid);
                roleMenu.setMid(Integer.valueOf(m));
                //4、增加角色对应的菜单
                roleMenu.insert();
            }
        };
        return new Result(insert,"");
    }

    @Override
    public Result updateRoleInfoService(String mids, Role role) {
        //1、更新角色信息
        boolean b = role.updateById();
        //2、更新角色的菜单权限
            //删除原有权限
                //创建条件构造器
                QueryWrapper<RoleMenu> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("rid",role.getRid());
            //增加新的菜单权限
            if (!"".equals(mids)&&mids!=null){
                String[] midStr = mids.split(",");
                //历遍完成增加
                for (String mid:midStr){
                    RoleMenu roleMenu=new RoleMenu(role.getRid(),Integer.parseInt(mid));
                    roleMenu.insert();
                }
            }

        return new Result(b,"");
    }

    @Override
    public Result deleteRoleInfoService(String rids) {
        //获取要删除的角色的ID
        String[] ridStr = rids.split(",");
        for (String rid:ridStr){
            //删除角色
            roleMapper.deleteById(Integer.parseInt(rid));
            //删除角色的菜单信息
                //创造构造器
                QueryWrapper<RoleMenu> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("rid",rid);
                roleMenuMapper.delete(queryWrapper);
        }
        return new Result(true,"");
    }
}
